# 2024/4/9 下午3:20 HFL
import time

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from api.logger import logger


class Keyword:

    def __init__(self):
        self.driver = webdriver.Chrome()

    # 打开浏览器输入URL
    def get(self, url: str):
        try:
            logger.info(f'访问地址---{url}----')
            return self.driver.get(url)
        except Exception as e:
            logger.error(f"访问---{url}--失败，错误信息：{e}")

    # 染色器，边框调色
    def locator_station(self, loc):
        self.driver.execute_script(
            'arguments[0].setAttribute("style", arguments[1])', loc,
            "border:4px solid red"  # 边框，红色
        )

    # 定位单个元素
    def find_element(self, loc, retry=1):
        for time1 in range(retry + 1):
            try:
                start_time = time.time()
                element = WebDriverWait(self.driver, 10, 1).until(EC.presence_of_element_located(loc))
                self.locator_station(element)
                end_time = time.time()
                logger.info(f"元素定位方法：{loc[0]}，元素位置：{loc[1]}--定位成功,耗时：{end_time - start_time, 3}")
                return element
            except Exception as e:
                error_info = f'元素：{loc}--定位失败，错误信息：{e}'
                logger.error(error_info)
                if time1 < retry:
                    logger.info(f'元素：{loc}正在重新定位，当前重试次数：{time1 + 1}')
                else:
                    raise Exception(error_info)

    # 定位多组元素
    def find_elements(self, loc, num: int, retry=1):
        for time1 in range(retry + 1):
            try:
                start_time = time.time()
                elements = WebDriverWait(self.driver, 10, 1).until(EC.presence_of_all_elements_located(loc))
                end_time = time.time()
                logger.info(f'元素定位方法：{loc[0]}，元素位置：{loc[1]}--定位成功，耗时：{end_time - start_time, 3}')
                return elements[num]
            except Exception as e:
                error_info = f'元素：{loc}--定位失败，错误信息：{e}'
                logger.error(error_info)
                if time1 < retry:
                    logger.info(f"元素：{loc}正在重新定位，当前重试次数：{time1 + 1}")
                else:
                    raise Exception(error_info)

    # 点击单个元素
    def click_element(self, loc):
        try:
            return self.find_element(loc).click(), logger.info(f'元素：{loc}--点击成功')
        except Exception as e:
            error_info = f'元素{loc}--点击失败,错误信息：{e}'
            logger.error(error_info)

    # 点击多组元素
    def click_elements(self, loc, num: int):
        try:
            return self.find_elements(loc, num).click(), logger.info(f'元素：{loc}--点击成功')
        except Exception as e:
            error_info = f'元素：{loc}--点击失败，错误信息：{e}'
            logger.info(error_info)

    # 输入框
    def input_text(self, loc, text):
        try:
            element = self.find_element(loc)
            element.clear()
            element.send_keys(text)
            logger.info(f'元素：{loc[1]}输入文本：{text}----输入成功')
        except Exception as e:
            error_info = f'元素：{loc[1]} 输入文本：{text}--输入失败，错误信息：{e}'
            logger.error(error_info)

    # iframe切换
    def switch_to_iframe(self, reference=None, retry=1):
        for time1 in range(retry + 1):
            try:
                start_time = time.time()

                if not reference:
                    self.driver.switch_to.default_content()
                end_time = time.time()
                self.driver.refresh()
                return self.driver.switch_to.frame(reference), logger.info(
                    f'iframe{reference}切换成功,耗时:{end_time - start_time, 3}')

            except Exception as e:
                error_info = f'iframe:{reference}切换失败，错误信息：{e}'
                logger.error(error_info)
                if time1 < retry:
                    logger.info(f'iframe：{reference}正在重新切换，当前重试次数：{time1 + 1} ')
                else:
                    raise Exception(error_info)

    # 窗口切换
    def switch_to_window(self):
        try:
            win = self.driver.window_handles
            self.driver.switch_to.window(win[-1])
            logger.info(f'窗口切换成功')
        except Exception as e:
            logger.error(f'窗口切换失败，错误信息：{e}')

    # 刷新页面
    def refresh(self):
        try:
            self.driver.refresh()
            logger.info(f'页面刷新成功')
        except Exception as e:
            logger.error(f"页面刷新失败，错误信息：{e}")
